package com.nk.system.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringEscapeUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.nk.system.beans.DropdownBean;
import com.nk.system.service.BachelorService;

/**
 * Servlet implementation class JsonCommonServlet
 */
public class JsonCommonServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Autowired
	private BachelorService bachelorService;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JsonCommonServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		response.setContentType("text/html;charset=UTF-8");

		PrintWriter out = response.getWriter();
		try {
			if("province".equals(request.getParameter("mode"))) {
				String result = jsonProvince(request);
				out.print(result);
			}else if("university".equals(request.getParameter("mode"))) {
				String result = jsonUniversity(request);
				out.print(result);
			}else if("faculty".equals(request.getParameter("mode"))) {
				String result = jsonFaculty(request);
				out.print(result);
			}else if("major".equals(request.getParameter("mode"))) {
				String result = jsonMajor(request);
				out.print(result);
			}
		}catch(Exception e) {
			e.printStackTrace();
		} finally {
			out.close();
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	private String jsonProvince(HttpServletRequest request) throws Exception {
		
		WebApplicationContext con = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
		bachelorService = (BachelorService)con.getBean("bachelorService");
		
		String zoneArr[] = request.getParameterValues("zoneSelect");
		/*String province = request.getParameter("province");
		String university = request.getParameter("university");
		String faculty = request.getParameter("faculty");*/
		List<String> zoneList = new ArrayList<String>();
		for(String zone:zoneArr) {
			zoneList.add(StringEscapeUtils.unescapeJava(zone));
		}
		
		List<DropdownBean> provinceName = bachelorService.getProvinceList(zoneList,1);
		/*List<DropdownBean> uniName = bachelorService.getUniversityList(zone, province, 1);
		List<DropdownBean> facultyName = bachelorService.getFacultyList(zone, province, university, 1);
		List<DropdownBean> majorName = bachelorService.getMajorList(zone, province, university, faculty, 1);*/
		
		return writeJson(provinceName);
	}
	
	private String jsonUniversity(HttpServletRequest request) throws Exception {
		
		WebApplicationContext con = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
		bachelorService = (BachelorService)con.getBean("bachelorService");
		
		String zoneArr[] = request.getParameterValues("zoneSelect");
		String provinceArr[] = request.getParameterValues("provinceSelect");

		List<String> zoneList = new ArrayList<String>();
		for(String zone:zoneArr) {
			zoneList.add(StringEscapeUtils.unescapeJava(zone));
		}
		List<String> provinceList = new ArrayList<String>();
		for(String province:provinceArr) {
			provinceList.add(StringEscapeUtils.unescapeJava(province));
		}

		List<DropdownBean> uniName = bachelorService.getUniversityList(zoneList, provinceList, 1);
		
		return writeJson(uniName);
	}
	
	private String jsonFaculty(HttpServletRequest request) throws Exception {
		
		WebApplicationContext con = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
		bachelorService = (BachelorService)con.getBean("bachelorService");
		
		String zoneArr[] = request.getParameterValues("zoneSelect");
		String provinceArr[] = request.getParameterValues("provinceSelect");
		String universityArr[] = request.getParameterValues("universitySelect");

		List<String> zoneList = new ArrayList<String>();
		for(String zone:zoneArr) {
			zoneList.add(StringEscapeUtils.unescapeJava(zone));
		}
		List<String> provinceList = new ArrayList<String>();
		for(String province:provinceArr) {
			provinceList.add(StringEscapeUtils.unescapeJava(province));
		}
		List<String> universityList = new ArrayList<String>();
		for(String university:universityArr) {
			universityList.add(StringEscapeUtils.unescapeJava(university));
		}

		List<DropdownBean> facultyName = bachelorService.getFacultyList(zoneList, provinceList, universityList, 1);
		
		return writeJson(facultyName);
	}
	
	private String jsonMajor(HttpServletRequest request) throws Exception {
		
		WebApplicationContext con = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
		bachelorService = (BachelorService)con.getBean("bachelorService");
		
		String zoneArr[] = request.getParameterValues("zoneSelect");
		String provinceArr[] = request.getParameterValues("provinceSelect");
		String universityArr[] = request.getParameterValues("universitySelect");
		String facultyArr[] = request.getParameterValues("facultySelect");

		List<String> zoneList = new ArrayList<String>();
		for(String zone:zoneArr) {
			zoneList.add(StringEscapeUtils.unescapeJava(zone));
		}
		List<String> provinceList = new ArrayList<String>();
		for(String province:provinceArr) {
			provinceList.add(StringEscapeUtils.unescapeJava(province));
		}
		List<String> universityList = new ArrayList<String>();
		for(String university:universityArr) {
			universityList.add(StringEscapeUtils.unescapeJava(university));
		}
		List<String> facultyList = new ArrayList<String>();
		for(String faculty:facultyArr) {
			facultyList.add(StringEscapeUtils.unescapeJava(faculty));
		}

		List<DropdownBean> majorName = bachelorService.getMajorList(zoneList, provinceList, universityList, facultyList, 1);
		
		return writeJson(majorName);
	}
	
	public String writeJson(Object mapObj) throws Exception {
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(mapObj);
	}

}
